from fastapi import APIRouter, Query

from app.controllers.dept import dept_controller
from app.schemas import Success
from app.schemas.depts import DeptCreate, DeptUpdate

router = APIRouter()


@router.get("/list", summary="查看部门列表")
async def tree_list(
    code: str = Query(None, description="部门编码"),
    name: str = Query(None, description="部门名称"),
    status: int = Query(None, description="状态"),
):
    """
    查看部门列表
    """
    rst = await dept_controller.tree_list(code, name, status)
    return Success(data=rst)


@router.get("/{dept_id}", summary="根据ID获取查看部门")
async def get_by_id(dept_id: int):
    """
    根据id查看部门
    """
    dept_obj = await dept_controller.get(id=dept_id)
    data = await dept_obj.to_dict()
    return Success(data=data)


@router.post("/", summary="创建部门")
async def create(obj: DeptCreate):
    """
    创建部门
    """
    await dept_controller.create(obj_in=obj)
    return Success(msg="Created Successfully")


@router.put(path="/", summary="修改部门")
async def update(obj: DeptUpdate):
    """
    修改部门
    """
    await dept_controller.update(id=obj.id, obj_in=obj)
    return Success(msg="Update Successfully")


@router.delete("/{dept_id}", summary="删除部门")
async def delete(dept_id: int):
    """
    删除部门
    """
    await dept_controller.delete(dept_id=dept_id)
    return Success(msg="Deleted Success")
